import {
  findUsedMaterialRecord,
  create,
  findMany,
  updateOne,
  deleteOne,
  review,
  findOne,
  exportExcel,
  history,
  uploadFile,
  findStatisticsMany,
  findRentMany,
  createRent,
  updateRent,
  deleteRent,
  findRent,
  payReview,
  createReturnGoodsOrder,
  findReturnOrderMany,
  deleteReturnGoodsOrder,
  updateReturnGoodsOrder, deleteSalesProd, createSalesProd, updateSalesProd, findSalesProd,
  updateReplenishRecord, deleteReplenishRecord, createReplenishRecord, findReplenishRecordMany, findSalesProdMany,findNumberTxt

} from '../services/sales';

export default {
  namespace: 'sales',
  state: {},

  effects: {
    * clearUsedMaterialRecord({payload}, {call, put}) {
      yield put({
        type: 'onDataLoad',
        payload: {
          usedMaterialRecord: []
        }
      });
    },
    * findUsedMaterialRecord({payload}, {call, put}) {
      const response = yield call(findUsedMaterialRecord, payload);
      if (!response) {
        yield put({
          type: 'onNotify',
          payload: {message: "无服务！", type: 'error'}
        });
        yield put({
          type: 'onDataLoad',
          payload: {
            usedMaterialRecord: []
          }
        });
        return;
      }

      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({
          type: 'onNotify',
          payload: {message, type: 'error'}
        });
        yield put({
          type: 'onDataLoad',
          payload: {
            usedMaterialRecord: []
          }
        });
        return message;
      }

      yield put({
        type: 'onDataLoad',
        payload: {
          usedMaterialRecord: result
        }
      });
      return response;

    },
    * create({payload}, {call, put}) {
      const response = yield call(create, payload);
      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return message;
        }
        yield put({
          type: 'onDataLoad',
          payload: {
            result
          }
        });
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },
    * findMany({payload}, {call, put}) {
      const response = yield call(findMany, payload);

      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {sales: result}
        });
        return;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },

    * findOne({payload}, {call, put}) {
      const response = yield call(findOne, payload);

      if (!response) {
        yield put({
          type: 'onNotify',
          payload: {message: "无服务！", type: 'error'}
        });
        return;
      }
      const {status, message, result} = response;
      const _payload = status !== "ok" ? message : {message: "操作成功！", type: 'info'};

      yield put({
        type: 'onNotify',
        payload: _payload
      });
      yield put({
        type: 'onDataLoad',
        payload: {
          salesOrder: result
        }
      });
    },
    * updateOne({payload}, {call, put}) {
      const response = yield call(updateOne, payload);
      console.log('updateOneee', response);
      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {
            result
          }
        });
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },

    * deleteOne({payload}, {call, put}) {
      const response = yield call(deleteOne, payload);
      if (response) {
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },
    * review({payload}, {call, put}) {
      const response = yield call(review, payload);
      if (!response) {
        yield put({
          type: 'onNotify',
          payload: {message: "无服务！", type: 'error'}
        });
        return;
      }

      const {status, message} = response;
      const _payload = status !== "ok" ? message : {message: "操作成功！", type: 'info'};

      yield put({
        type: 'onNotify',
        payload: _payload
      });

    },

    * payReview({payload}, {call, put}) {
      const response = yield call(payReview, payload);
      if (!response) {
        yield put({
          type: 'onNotify',
          payload: {message: "无服务！", type: 'error'}
        });
        return;
      }

      const {status, message} = response;
      const _payload = status !== "ok" ? message : {message: "操作成功！", type: 'info'};

      yield put({
        type: 'onNotify',
        payload: _payload
      });

    },
    * exportExcel({payload}, {call, put}) {
      const response = yield call(exportExcel, payload);
      if (response) {
        const {status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: message
          });
          return;
        }
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },
    * history({payload}, {call, put}) {
      const response = yield call(history, payload);
      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: message
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {
            orderHistory: result
          }
        });
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
        return;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },

    * uploadFile({payload}, {call, put}) {
      const response = yield call(uploadFile, payload);

      if (!response) {
        yield put({
          type: 'onNotify',
          payload: {message: "无服务！", type: 'error'}
        });
        return;
      }

      const {status, message} = response;
      const _payload = status !== "ok" ? message : {message: "操作成功！", type: 'info'};

      yield put({
        type: 'onNotify',
        payload: _payload
      });

    },

    * findStatisticsMany({payload}, {call, put}) {
      const response = yield call(findStatisticsMany, payload);
      console.log('findStatisticsMany', response);
      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {
            result
          }
        });
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });

    },

    * findRentMany({payload}, {call, put}) {
      const response = yield call(findRentMany, payload);
      console.log('model findRentMany', response);
      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {rents: result}
        });
        return response;
      }
    },
    * findRent({payload}, {call, put}) {
      const response = yield call(findRent, payload);
      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {rent: result}
        });
        return response;
      }
    },
    * createRent({payload}, {call, put}) {
      return yield call(createRent, payload);
    },
    * deleteRent({payload}, {call, put}) {
      return yield call(deleteRent, payload);
    },
    * updateRent({payload}, {call, put}) {
      return yield call(updateRent, payload);
    },

    /**
     * 退补货
     */
    // router.post('/api/sales/returnOfGoods/find/many', controller.sales.findReturnOrderMany);
// router.post('/api/sales/returnOfGoods/create', controller.sales.createReturnGoodsOrder);
// router.post('/api/sales/returnOfGoods/delete', controller.sales.deleteReturnGoodsOrder);
// router.post('/api/sales/returnOfGoods/update', controller.sales.updateReturnGoodsOrder);
//
// router.post('/api/sales/replenish/find/many', controller.sales.findReplenishRecordMany);
// router.post('/api/sales/replenish/create', controller.sales.createReplenishRecord);
// router.post('/api/sales/replenish/delete', controller.sales.deleteReplenishRecord);
// router.post('/api/sales/replenish/update', controller.sales.updateReplenishRecord);

    * findReturnOrderMany({payload}, {call, put}) {
      const response = yield call(findReturnOrderMany, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }

      yield put({type: 'onDataLoad', payload: {returnGoods: result}});
      return response;
    },
    * createReturnGoodsOrder({payload}, {call, put}) {
      const response = yield call(createReturnGoodsOrder, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }

      yield put({type: 'onDataLoad', payload: {}});
      return response;
    },
    * deleteReturnGoodsOrder({payload}, {call, put}) {
      const response = yield call(deleteReturnGoodsOrder, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }

      yield put({type: 'onDataLoad', payload: {}});
      return response;
    },
    * updateReturnGoodsOrder({payload}, {call, put}) {
      const response = yield call(updateReturnGoodsOrder, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }

      yield put({type: 'onDataLoad', payload: {}});
      return response;
    },
    * findReplenishRecordMany({payload}, {call, put}) {
      const response = yield call(findReplenishRecordMany, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }

      yield put({type: 'onDataLoad', payload: {replenishRecords: result}});
      return response;
    },
    * createReplenishRecord({payload}, {call, put}) {
      const response = yield call(createReplenishRecord, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }

      yield put({type: 'onDataLoad', payload: {}});
      return response;
    },
    * deleteReplenishRecord({payload}, {call, put}) {
      const response = yield call(deleteReplenishRecord, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }

      yield put({type: 'onDataLoad', payload: {}});
      return response;
    },
    * updateReplenishRecord({payload}, {call, put}) {
      const response = yield call(updateReplenishRecord, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }
      yield put({type: 'onDataLoad', payload: {}});
      return response;
    },
    * findSalesProd({payload}, {call, put}) {
      const {spLotId} = payload;
      console.log('findSalesProd', spLotId);
      if (!spLotId) {
        yield put({type: 'onDataLoad', payload: {salesProds: {}}});
        return;
      }
      const response = yield call(findSalesProd, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }
      console.log('findSalesProdMany', result);
      yield put({type: 'onDataLoad', payload: {salesProd: result}});
      return response;
    },
    * findSalesProdMany({payload}, {call, put}) {
      const response = yield call(findSalesProdMany, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }
      console.log('findSalesProdMany', result);
      yield put({type: 'onDataLoad', payload: {salesProds: result}});
      return response;
    },
    * createSalesProd({payload}, {call, put}) {
      const response = yield call(createSalesProd, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }
      yield put({type: 'onDataLoad', payload: {}});
      return response;
    },
    * deleteSalesProd({payload}, {call, put}) {
      const response = yield call(deleteSalesProd, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }
      yield put({type: 'onDataLoad', payload: {}});
      return response;
    },
    * updateSalesProd({payload}, {call, put}) {
      const response = yield call(updateSalesProd, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }
      yield put({type: 'onDataLoad', payload: {}});
      return response;
    },
    * findNumberTxt({payload}, {call, put}) {
      const response = yield call(findNumberTxt, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }

      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }
      yield put({type: 'onDataLoad', payload: {numberTxt: result}});
      return response;
    }

  },

  reducers: {
    onDataLoad(state, {payload}) {
      state._notify = undefined;
      return {...state, ...payload};
    },
    onNotify(state, {payload}) {
      state._notify = payload;
      console.log('resss',payload)
      return {...state, ...payload};
    }
  }
};
